<?php

$db = new Monitoring();
$id = mysql_real_escape_string($_GET['id']);
$table = "monitoring";
$detail = $db->lihat_monitoring($table,$id);

$o .= '<form class="form-horizontal" enctype="multipart/form-data" id="registerHere" method="post" action="?page=editmonitoring&id='.$id.'&action=edit">
		<fieldset>
			<legend>MONITORING</legend>';

	

/* alert yang muncul setelah proses */
if (isset($_GET['success']) && ($_GET['success'] == "1")) {

	$o .= '<div class="alert alert-success">
			Proses update monitoring berhasil
			</div>';
			
} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
	
	$o .= '<div class="alert alert-error">
			Proses update monitoring gagal
		   </div>';
} elseif (isset($_GET['upload']) && ($_GET['upload'] == "0")) {
	
	$o .= '<div class="alert alert-error">
			Proses upload gambar gagal
		   </div>';
}
	
foreach ($detail as $x) {

	$o .= '<div class="control-group">
		  <label class="control-label" for="input01">Nama Kegiatan</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="kegiatan" name="kegiatan" value="'.$x['nama_kegiatan'].'" rel="popover" data-content="Masukkan nama kegiatan." data-original-title="Kegiatan" readonly=readonly >
			<input type="hidden" name="idkegiatan" value="'.$x['idkegiatan'].'">
		  </div>
		</div>
		<div class="control-group">
            <label class="control-label" for="fileInput">Photo</label>
            <div class="controls">
              <input class="input-file" name="image" id="fileInput" type="file">
			  <input class="input-file" name="gambar_lama" value="'.$x['foto'].'" type="hidden">
            </div>
          </div>
          
         <div class="control-group">
            <label class="control-label" for="progress">Progress</label>
            <div class="controls">
				<select name="progress">
				<option value="'.$x['progres'].'">'.$x['progres'].' %</option>
				<option value="25">25 %</option>
				<option value="50">50 %</option>
				<option value="75">75 %</option>
				<option value="100">100 %</option>
				</select>
            </div>
          </div>
            
		<div class="control-group">
			<label class="control-label" for="input01"></label>
		<div class="controls">
		   <button type="submit" class="btn btn-success" >Ubah Monitoring</button>
		  </div>
		</div>
		  </fieldset>
		</form>';
}
	echo $o;
?>
	



<a href="?page=kegiatan">kembali</a>

<?php


if ($_GET['action'] == "edit") {
	
	require ('../plugins/SimpleImage.php');
	
	// standarisasi ukuran foto yang di upload
	$dim  = array('W'=>800, 'H'=>600);
	$dim2 = array('W'=>160, 'H'=>120);
	
	$uploaddir 		= "../file/$picName";
	$thumbdir  		= "../file/thumbnail/$picName";
	$picName 		= basename($_FILES['image']['name']);
	$idkegiatan		= htmlentities(mysql_real_escape_string($_POST['idkegiatan']));
	$progress		= htmlentities(mysql_real_escape_string($_POST['progress']));
	$date			= date('Y-m-d');
	$username		= $_SESSION['username'];
	
	// get uploaded foto
	if (is_uploaded_file($_FILES['image']['tmp_name'])) {
		
		if((copy($_FILES['image']['tmp_name'], $uploaddir.$picName))) {
			
			// if dimension not standard, resize and clip it
			// include class for resize image into double
			// define default image type
			$imgType = IMAGETYPE_JPEG;
			// resize image if dimension out of range
			$imgDms = getimagesize($_FILES['image']['tmp_name']);
			if ($imgDms[0] > $dim['W']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToWidth($dim['W']);
				$image->save($uploaddir.$picName, $imgType);
			} elseif ($imgDms[1] > $dim['H']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToHeight($dim['H']);
				$image->save($uploaddir.$picName, $imgType);
			}
					
			
			if (is_uploaded_file($_FILES['image']['tmp_name'])) {
		
				if((copy($_FILES['image']['tmp_name'], $thumbdir.$picName))) {
			
					// if dimension not standard, resize and clip it
					// include class for resize image into double
					// define default image type
					$imgType = IMAGETYPE_JPEG;
					// resize image if dimension out of range
					$imgDms = getimagesize($_FILES['image']['tmp_name']);
					if ($imgDms[0] > $dim2['W']) {
						$image = new SimpleImage();
						$image->load($thumbdir.$picName);
						$image->resizeToWidth($dim2['W']);
						$image->save($thumbdir.$picName, $imgType);
					} elseif ($imgDms[1] > $dim2['H']) {
						$image = new SimpleImage();
						$image->load($thumbdir.$picName);
						$image->resizeToHeight($dim2['H']);
						$image->save($thumbdir.$picName, $imgType);
					}
				}
				
			} 
		} 
	} else {
				$db = new Monitoring();
				$id = mysql_real_escape_string($_GET['id']);
				$table = "monitoring";
				$field = array('idkegiatan'=>$idkegiatan,'tgl_monitoring'=>$date,'progres'=>$progress,'foto'=>$picName);
				$monitoring = $db->ubah($id,$table,$field);
				
				if ($monitoring) {
					echo '
						<script language="javascript">
						document.location="?page=monitoring&id='.$id.'&success=1";
						</script>';
					
				} else {
					echo '
						<script language="javascript">
						document.location="?page=monitoring&id='.$id.'&success=0";
						</script>';
				}
			}	
			
				$db = new Monitoring();
				$id = mysql_real_escape_string($_GET['id']);
				$table = "monitoring";
				$field = array('idkegiatan'=>$idkegiatan,'tgl_monitoring'=>$date,'progres'=>$progress,'foto'=>$_POST['gambar_lama']);
				$monitoring = $db->ubah($id,$table,$field);
				
				if ($monitoring) {
					echo '
						<script language="javascript">
						document.location="?page=monitoring&id='.$id.'&success=1";
						</script>';
					
				} else {
					echo '
						<script language="javascript">
						document.location="?page=monitoring&id='.$id.'&success=0";
						</script>';
				}
	}		



?>
